package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherSearch;
import com.atguigu.guli.service.edu.service.OssFeignClient;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-07-27
 */
//允许跨域访问
@Api(tags = "讲师管理模块")
@RestController
@RequestMapping("/admin/edu/teacher")
@Slf4j
@RefreshScope//表示要动态监听配置中心得配置
public class AdminTeacherController {

    @Autowired
    TeacherService teacherService;
    @Autowired
    OssFeignClient ossFeignClient;
    //测试远程调用
    @GetMapping("test")
    public R test(){
//        ossFeignClient.test("admin","zsyal");
        ossFeignClient.test(ResultCodeEnum.FETCH_PLAYAUTH_ERROR);
        return R.ok();
    }






    //7.批量删除
    @DeleteMapping("batchDel")
    //前端提交的时json数组  所以这边要用@RequestBody
    public R batchDel(@RequestBody List<String> ids){
        teacherService.removeByIds(ids);
        return R.ok();
    }

    @Value("${myName}")
    String myName;
    @Value("${myRedisHost}")
    String myRedis;
    //1.查询所有
    @ApiOperation(value = "查询所有讲师")
    @GetMapping("list")
    public R listAll(){
        System.out.println("myName="+myName);
        System.out.println("myRedis="+myRedis);
        List<Teacher> teachers = null;

        teachers = teacherService.list();

        log.debug("当前打印的日志级别：{},时间为{}" , "debug",new Date());//测试时使用
        log.info("当前打印的日志级别：{},时间为{}" , "debug",new Date());//开发环境使用
        log.warn("当前打印的日志级别：{},时间为{}" , "debug",new Date());//生产环境使用
        log.error("当前打印的日志级别：{},时间为{}" , "debug",new Date());
        return R.ok().data("items",teachers);
    }

    //2.带条件的分页查询
    @ApiOperation(value = "查询讲师分页列表")
    @GetMapping("list/{pageNum}/{pageSize}")
    public R listPage(@ApiParam(value = "页码",required = true,defaultValue = "1") @PathVariable("pageNum") Integer pageNum,
                      @ApiParam(value = "每页显示条数",required = true,defaultValue = "3") @PathVariable("pageSize") Integer pageSize,
                      TeacherSearch teacherSearch){
        Page<Teacher> page =  teacherService.selectPageByCondition(pageNum,pageSize,teacherSearch);

        return R.ok().data("page",page);
    }
    //3.新增讲师
    @ApiOperation(value = "新增讲师")
    @PostMapping("save")
    public R saveTeacher(@RequestBody Teacher teacher){
        boolean b = teacherService.save(teacher);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //4.删除讲师
    @ApiOperation(value = "删除讲师")
    @DeleteMapping("deleteById/{id}")
    public R deleteById(@PathVariable("id") String id){
        //删除讲师的同时删除讲师头像
        //远程访问service-oss 删除文件的接口：文件地址  模块（目录名称）avatar
        Teacher teacher = teacherService.getById(id);
        String path = teacher.getAvatar();
        if (!StringUtils.isEmpty(path)){
            ossFeignClient.deleteFile(path,"avatar");

        }

        boolean b = teacherService.removeById(id);
        if (b){
            return R.ok();
        }else {
            return R.error().message("记录不存在");
        }
    }

    //5.根据id查询讲师
    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("getById/{id}")
    public R getById(@ApiParam("讲师id")@PathVariable("id") String id){
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("item",teacher);
    }

    //6.更新讲师
    @ApiOperation(value = "更新讲师")
    @PutMapping("updateById")
    public R updateById(@RequestBody Teacher teacher){
        teacherService.updateById(teacher);
        return R.ok();
    }
}

